import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';
import { Router,ActivatedRoute} from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
    selector: 'sa-dataCheck',
    templateUrl: './dataCheck.component.html',
    styleUrls:['./dataCheck.component.css']
})
export class DataCheckComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	carList:any[] = [];
	companyId:string = '';
	deviceId:string = '';//设备ID
	startTime_table:string = '';
	startTime_chart:string = '';
	endTime_table:string = '';
	endTime_chart:string = '';
	abnormalType='1';
	minExceptionTime='';//最小异常时间
	carNumber:string = ''; //车牌号码
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	activeSelected:string='';
	selectedRow:any;
	isChartView=false;
	tableHeardlist=[
		{keys:'type',value:'修复类型 '},
		{keys:'carNumber',value:'车牌号码'},
		{keys:'startTime',value:'开始时间'},
		{keys:'endTime',value:'结束时间'},
		{keys:'userNameForSub',value:'修复人员'},
		{keys:'userNameForCheck',value:'审核人员'},
		{keys:'isCheck',value:'状态'},
		{keys:'createTime',value:'创建时间'},
		{keys:'checkTime',value:'审核时间'},
		{keys:'remark',value:'备注'},
	];
	tableQueryitem={
		type:'', 				//日志类型：1：重量修复，2：轨迹修复
		isCheck:'',				// 审核状态：0：未审核，1：已审核
		companyId:'',			//公司ID
		startTime:'',			//开始时间
		endTime:'',				// 结束时间
		searchKey:'',			//模糊搜索：按设备ID，车牌号码，提交日志用户名
	}
	checkRemark='';
	//画轨迹的数据
	drawTrackArr:any;
	isMapView=false;
	specifyId={
		picker1:'datetimepicker3',
		picker2:'datetimepicker4'
	}
	specifyTime={
		startTime:'',
		endTime:'',
	}
	constructor(private jsonApiService: JsonApiService,private router: Router) {}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.tableQueryitem.startTime=time.startTime;
		this.tableQueryitem.endTime=time.endTime;
	} 
	getSelectChartTime(time){
		this.startTime_chart=time.startTime;
		this.endTime_chart=time.endTime;
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
	}
	runParent(value){
	   this.drawTrackArr=value;
	}
	showMapView(val){
		this.isMapView=val;
	}
	checkAbnormal(row,modal){
		modal.show(1);
		this.activeSelected=row.id;
		this.startTime_chart=row.startTime;
		this.endTime_chart=row.endTime;
		this.isChartView=true;
		this.specifyTime={
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
		}
		this.selectedRow=row;
	}
	ngOnInit() {
		this.load()
	}
	drawAbnormalChart(){
		if(!this.selectedRow){
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		if(!this.startTime_chart||!this.endTime_chart){
			this.jsonApiService.message('异常起始时间不能为空');
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime_chart,this.endTime_chart,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let params={
			deviceId:this.selectedRow.deviceId,
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
			type:this.selectedRow.type,
		}
		this.selectedRow=params;
		
		
	}
	
	setCheckrowMes(row){
		this.selectedRow=row;
	}
	//审核
	checkRow(checkType){
		if(!this.selectedRow) return;
//		this.jsonApiService.confirm_message("确定该信息无误并审核通过吗?").then(()=>{
			let params={
				id:this.selectedRow.id,
				isCheck:checkType,
				remark:this.checkRemark,
			}
			let reqdata='';
			for(let keys in params){
				reqdata=reqdata+keys+'='+params[keys]+'&';
			}
			reqdata=reqdata.substring(0,reqdata.length-1);
			this.jsonApiService.http("fixdatalog/checkfixdatabyid.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.checkRemark='';
				this.load();
			})
//		})
	}
	//模板里面的快速审核
	modalCheckEvent(){
	if(!this.selectedRow) return;
	this.jsonApiService.confirm_message("确定该信息无误并审核通过吗?").then(()=>{
		let params={
			deviceId:this.selectedRow.deviceId,
			startTime:this.selectedRow.startTime,
			endTime:this.selectedRow.endTime,
			type:this.selectedRow.type,
		}
		let reqdata='';
		for(let keys in params){
			reqdata=reqdata+keys+'='+params[keys]+'&';
		}
		reqdata=reqdata.substring(0,reqdata.length-1);
		this.jsonApiService.http("fixdatalog/checkfixdata.html", reqdata).subscribe(data => {
			this.jsonApiService.message(data.message);
			this.load();
		})
	})
		
		
		
	}
	revoke_row(row){
		this.jsonApiService.confirm_message("确认撤销该条修复记录吗?").then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("fixdatalog/recallfixdatabyid.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//跳转到数据优化页面
	jumpOptimization(row){
		var junmpData={
				deviceId:row.deviceId,
				carNumber:row.carNumber,
				startDate:row.startDate,
				endDate:row.endDate
			}
		this.router.navigate(['/system-config/data-optimization'],{ queryParams:junmpData});
		setTimeout(function(){
			$('#left-panel').find('.top-menu-invisible.open').find('.UL').css('display','none');
			$('#left-panel').find('.top-menu-invisible.open').removeClass('open');
			$('#left-panel').find('.top-menu-invisible.active').addClass('open');
			$('#left-panel').find('.top-menu-invisible.active').find('.UL').css('display','block');
		},300)
	}
	delete_row(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("dataExceptionMonitor/deleteDataInfoById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//  初始化加载list数据
	load() {
		let timeCheck=common.correctTimeParams(this.tableQueryitem.startTime,this.tableQueryitem.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				type:this.tableQueryitem.type,
				isCheck:this.tableQueryitem.isCheck,
				companyId:this.selectedCompanyId,
				startTime:this.tableQueryitem.startTime,
				endTime:this.tableQueryitem.endTime,
				searchKey:this.tableQueryitem.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("fixdatalog/fixdatacheckforpage.html", reqdata).subscribe(result => {
			if(!result.isSuccess){
				this.jsonApiService.message(result.message);
				return;
			}
			this.tableData = result.exhibitDatas;
			this.totalCount = result.recordCount;
		})
	}
	ngOnDestroy(){
		document.onclick = null;
	}
	//刷新
    refresh(){
    	this.load();
    }
	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	closeChart(){
		this.isChartView=false;
	}
}
